package cn.yunhe;

import cn.yunhe.dao.AdminDao;
import cn.yunhe.dao.LiuYanDao;
import cn.yunhe.entity.Admin;
import cn.yunhe.entity.LiuYan;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.Date;
import java.util.List;

public class MybatisTest {

    SqlSession session=SqlSessionFactoryUtil.getSqlSession();

    @Test
    public void test1(){
        LiuYanDao liuYanDao= session.getMapper(LiuYanDao.class);
        List<LiuYan> liuYanList=liuYanDao.getAllLiuYan();

        for (LiuYan l : liuYanList) {
            System.out.println(l.getId()+"   "+l.getTitle()+"   "+l.getContext());
        }
    }

    @Test
    public void test2(){
        LiuYan liuYan=new LiuYan();
        liuYan.setId(3);
        LiuYanDao liuYanDao= session.getMapper(LiuYanDao.class);
        List<LiuYan> liuYanList=liuYanDao.getKeyLiuYan(liuYan);

        for (LiuYan l : liuYanList) {
            System.out.println(l.getId()+"   "+l.getTitle()+"   "+l.getContext());
        }
    }

    @Test
    public void test3(){
        LiuYan liuYan=new LiuYan();
        liuYan.setTitle("Mybatis66666");
        liuYan.setContext("厉害厉害666666");
        liuYan.setCreatedate(new Date());
        Admin admin=new Admin();
        admin.setId(1);
        liuYan.setAdmin(admin);
        LiuYanDao liuYanDao= session.getMapper(LiuYanDao.class);
        int a=liuYanDao.addLiuYan(liuYan);
        System.out.println(a);
        session.commit();
    }

    @Test
    public void test4(){
        LiuYan liuYan=new LiuYan();
        liuYan.setTitle("Mybatis牛皮啊");
        liuYan.setContext("真的是666666");
        liuYan.setId(17);
        LiuYanDao liuYanDao= session.getMapper(LiuYanDao.class);
        int a=liuYanDao.updateLiuYan(liuYan);
        System.out.println(a);
        session.commit();
    }

    @Test
    public void test5(){
        LiuYanDao liuYanDao= session.getMapper(LiuYanDao.class);
        int a=liuYanDao.deleteAdmin(12);
        System.out.println(a);
        session.commit();
    }

    @Test
    public void test6(){
        LiuYanDao liuYanDao= session.getMapper(LiuYanDao.class);
        List<LiuYan> liuYanList=liuYanDao.getLiuYanName();
        for (LiuYan l : liuYanList) {
            System.out.println(l.getTitle()+"   "+l.getAdmin().getName());
        }
    }


    @Test
    public void test7(){
        LiuYanDao liuYanDao= session.getMapper(LiuYanDao.class);
        LiuYan liuYan=new LiuYan();
        liuYan.setId(7);
        Admin admin=new Admin();
        admin.setId(2);
        liuYan.setAdmin(admin);
        List<LiuYan> liuYanList=liuYanDao.xiaoBai(liuYan);
        for (LiuYan l : liuYanList) {
            System.out.println(l.getTitle()+"   "+l.getAdmin().getId());
        }
    }


    @Test
    public void Login(){
        String name="admin";
        String pass="admin";
        AdminDao adminDao=session.getMapper(AdminDao.class);
        Admin admin=new Admin();
        admin.setName(name);
        admin.setPass(pass);
        Admin aa=adminDao.getAdmin(admin);
        if (aa!=null){
            System.out.println("登陆");
            System.out.println(aa.getName()+"   "+aa.getId());
            for (LiuYan l:aa.getLiuYans()) {
                System.out.println(l.getTitle());
            }

        }else{
            System.out.println("唉");
        }
    }


    public void test8(){

    }

}